package com.kun.index.controller;


import com.kun.common.ResultDto;
import com.kun.common.annotation.PermissionAuthentication;
import com.kun.index.controller.dto.LoginDto;
import com.kun.utils.DataSourceUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/index/system")
public class SystemController {

    @Value("${spring.datasource.username}")
    private String userName;            //用户名

    @Value("${diy.common.host}")
    private String host;                //主机名称

    @Value("${diy.common.mysqlDockerName}")
    private String mysqlDockerName;                //mysql的容器名称

    @Value("${spring.datasource.password}")
    private String password;            //密码

    @Value("${diy.common.database}")
    private String database;            //数据库名称

    @Value("${file.download.sqlFilePath}")
    private String backupFolderPath;            //备份文件路径

    @Value("${file.download.sqlFileName}")
    private String fileName;            //备份文件名称


    @GetMapping("/backup")
    @ApiOperation("备份数据库")
    public ResultDto backup() throws Exception {
        boolean flag = DataSourceUtils.backupMysqlDataSource(host,mysqlDockerName, userName, password, backupFolderPath, fileName, database);
        if (flag){
            return ResultDto.success("数据备份成功");
        }
        return ResultDto.fail("备份失败,请联系管理员");
    }

    @Scheduled(cron = "0 0 0 * * * ")
//    @Scheduled(cron = "0 7 18 * * * ")
    @ApiOperation("定时执行数据库备份任务")
    public void scheduledBackupDataSource(){
        try {
            this.backup();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }




}
